/**
 * 定义站点地图模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.sw.bcDeviceConfig.bcDeviceConfigManager', [
	                                                                       'water.entity.bcDrive',
	                                                                       'water.entity.bcDevice',
	                                                                       'water.entity.bcDeviceConfig']);
		
	module.controller('bcDeviceConfigManagerController',function($scope,$state,$stateParams,$bcDriveService,$bcDeviceConfigService,$bcDeviceService){

		/**
		 * 传过来的设备ID
		 */
		$scope.deviceId = $stateParams.deviceId;
		
		/**
		 * 驱动参数元数据
		 */
		$scope.parameterMetadata = {};
		
		/**
		 * 设备设置
		 */
		$scope.deviceConfig = [];
		
		/**
		 * 返回按钮点击事件
		 */
		$scope.onGoBackClick = function(){
			$state.go("bcDevice");
		}
		
		/**
		 * 保存点击事件
		 */
		$scope.onSaveClick = function(deviceConfig){
			$bcDeviceConfigService.creates(deviceConfig).then(function(){
				alert("保存成功");
				$state.go("bcDevice");
			});
		}
		
		/**
		 * 获取驱动配置元数据
		 */
		$scope.getParameterMetadata = function(deviceId){
			
			$bcDeviceService.getModel(deviceId).then(function(device){
				
				var driveId = device[0].PROTOCOL_ID;
				
				$bcDriveService.getParameterMetadata(driveId).then(function(data){
					
					for(var i=0;i<data.length;i++){
						var item = data[i];
						$scope.parameterMetadata[item.name] = item;
					}
					
				});
				
			});
		}
		
		/**
		 * 获取驱动的配置信息
		 */
		$scope.getDeviceConfig = function(devicceId){
			$bcDeviceConfigService.getList(devicceId).then(function(data){
				$scope.deviceConfig = data;
			});
		}
		
		$scope._init = function(){
			$scope.getParameterMetadata($scope.deviceId);
			$scope.getDeviceConfig($scope.deviceId);
		}
		
		$scope._init();
	});
	
}(window, angular));


